<?php 
//Google Talk Status to Twitter
//Compiled by Jon Deutsch
//http://www.jdeutsch.com


	// return type of service
	function gtalk_getType() { return "status"; }

	// extra the service- action  line ("ike: "I tweeted on twitter.com")
	function gtalk_getAction($line) {
		return "<a href='?service=gtalk'><img class='icon' src='".serviceIcon("gtalk")."'/></a> My status on <a href='http://google.com/talk'>google talk</a>";
	}


	// extract and return the correct caption
	function gtalk_getCaption($line) {
		$entry=explode("|",$line);
		return $entry[3];
	}

	// extract and return the correct post
	function gtalk_getPost($line) {
		return "";
	}

	// extract and return the correct link to the item
	function gtalk_getLink($line) {
		$entry=explode("|",$line);
		return $entry[5];
	}	

	// display information about us with a link to the profile
	//  -- this is called to display list of services that are linked (admin-functions.php)
	function gtalk_identify($item) {
		$entry=explode("|",$item);
		return  "GTalk status"; //: <a href='".$entry[3]."'>".$entry[2]."</a>";
	}	
	
	
	function gtalk_form() {
		print "Badge code: <input type='text' name='username'> <input type='submit' name='add' value='add'><br/>";
		print "Find your badge code here: <a target='_blank' href='http://www.google.com/talk/service/badge/New'>http://www.google.com/talk/service/badge/New</a><Br/>";
		print "Make sure to enable 'Show your status message'";
	}

	// get service + profile information
	function gtalk_addProfile() {
			$serviceID=$_POST['addservice'];
			$username=$_POST['username'];
	
			//Pull the gtalk badge
			$url=stripslashes($_POST["username"]);
			
			$rsslink=substr($url,strpos($url,"src=")+5);
			$url=$rsslink;

			$rsslink=substr($url,0,strpos($url,'"'));
			$username=$rsslink;
			
			
			writeService($serviceID,$username,$rsslink);
	}

	// download latest items from this service
	function gtalk_download($serviceid,$filename)  {
		$output = @file($filename); //curl_exec($ch);
		
		// close curl resource, and free up system resources
		//curl_close($ch);
		$beginstring = '<img src="/talk/service/resources/blank.gif" height="9" width="9" alt="">';
		$endstring = '</div>';
		
		$line="";
		while (count($output)) {
			$line.=array_shift($output);
		}


	//Parse the string in the gtalk badge to find the status
		$beginposition = strlen($beginstring) + strpos($line, $beginstring)+1;
		$endposition = strpos(   substr($line,$beginposition)   , $endstring);
		
		$status = substr($line,$beginposition,$endposition);

		// check if this is a new status
		
		$newStatus=true;
		$fname=$blogRoot."txts/__gtalk.txt";
		if (file_exists($fname) && filesize($fname)!=0) {
			$newStatus=false;
			
			$load = @file($fname);
			$line=array_shift($load);
			if ($line!=$status && $line!="Offline") $newStatus=true;
		}
		
		if ($newStatus) {
			$open = fopen($fname, "w");
			fwrite($open,$status);
			fclose($open);
			
			$indx=date("YmdHis",time());
			$item[$indx]->title=$status;
			$item[$indx]->post=$status;
			$item[$indx]->pubDate=$indx;
			$item[$indx]->guid=$status;

		}
		
		insertEntries($item,"gtalk",$serviceid);
	}

?>
